package com.yesjpt.serv.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.springframework.stereotype.Service;
import com.yesjpt.serv.ISmsServ;
import com.yesjpt.util.L;

/*
 * 发送短信
 */
@Service
public class SmsServImpl extends BaseServImpl implements ISmsServ {

	private static String smsServer = ""; //短信运营商URL
	private static String smsUsername = "";//注册用户名
	private static String smsPwd = "";//密码
	
	//替换敏感词
	public String replaceSensitiveWords(String content) {
		/*
		char c = '*';
		// L.i(new Throwable().getStackTrace()[0] + "，原始内容：" + s + "\n敏感词分隔符：" + c);
		for (Dic d : dicServ.listByType("敏感词")) {
			String p = d.getName().trim().replace(" ", "");
			if (content.indexOf(p) >= 0) {
				System.out.println("替换敏感词：" + p);
				content = content.replace(p, StringUtil.replaceSensitiveWords(p, c));
			}
		}
		content = content.replace("【", "(");
		content = content.replace("】", ")");
		// L.i(new Throwable().getStackTrace()[0] + "新内容：" + s);
		 */
		return content;
	}

	@Override
	public void hello() {
		System.out.println("hello");
	}

	public String sendMsg(String phone, String content) {
		content = replaceSensitiveWords(content);
		L.i(new Throwable().getStackTrace()[0] + "，phone = " + phone + ", content = " + content + ", 长度 = " + content.length());
		StringBuffer sb = new StringBuffer();
		try {// 发送短信
			String urlString = smsServer + "/send/gsend.asp?name=" + smsUsername + "&pwd=" + smsPwd + "&dst=" + phone + 
					"&msg=" + URLEncoder.encode(content, "GB2312") + "&txt=ccdx";
			URL url = new URL(urlString);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GB18030"));
			for (String line = null; (line = reader.readLine()) != null;)
				sb.append(line + "\n");
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		L.i(new Throwable().getStackTrace()[0] + "，短信发送结果 = " + sb.toString());
		return sb.toString();
	}

	// 查询余额
	public String getFee() {
		String sUrl = null;
		sUrl = smsServer + "/send/getfee.asp?name=" + smsUsername + "&pwd=" + smsPwd;
		return getUrl(sUrl);
	}

	// 读取短信
	public String readMsg() {
		String urlString = null;
		urlString = smsServer + "/send/readsms.asp?name=" + smsUsername + "&pwd=" + smsPwd;
		try {
			URLEncoder.encode(urlString, "GB2312");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return getUrl(urlString);
	}

	public String getUrl(String urlString) {
		StringBuffer sb = new StringBuffer();
		try {
			URL url = new URL(urlString);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			for (String line = null; (line = reader.readLine()) != null;)
				sb.append(line + "\n");
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	// 定时发送time = YYYYMMDDHHMM，subNo当前为""
	public String massSend(String phone, String msg, String time, String subNo, String txt) {
		String url = null;
		subNo = "";
		try {
			url = smsServer + "/send/gsend.asp?name=" + smsUsername + "&pwd=" + smsPwd + "&dst=" + phone + "&msg="
					+ URLEncoder.encode(msg, "GB2312") + "&time=" + time + "&sender=" + subNo + "&txt=" + txt;// 这里必须GB2312否则发到手机乱码
		} catch (Exception e) {
			e.printStackTrace();
		}
		return getUrl(url);
	}

}
